package com.wise.site.main;

import com.wise.site.R;
import com.wise.site.WiseSiteApplication;
import com.wise.site.adapter.IndexerBarAdapter;
import com.wise.site.adapter.InfoListAdatper;
import com.wise.site.adapter.InfoListAdatper.ITEM_TYPE;
import com.wise.site.detail.ShopsDetailsActivity;
import com.wise.site.protocol.action.ShopListAction;
import com.wise.site.protocol.base.ProtocolManager;
import com.wise.site.protocol.result.IndexerBarEntry;
import com.wise.site.protocol.result.ModelResult;
import com.wise.site.protocol.result.ShopItem;
import com.wise.site.protocol.result.TabItem;
import com.wise.site.utils.Constanct;
import com.wise.site.widget.IndexerBarGallery;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 商铺列表
 * 
 * @author huangyue
 * 
 *         2012-9-21
 */
public class ShopActivity extends Activity implements OnClickListener {
	private InfoListAdatper headlineShop;
	private ListView mListView;
	private IndexerBarAdapter mGalleryAdapter;
	private IndexerBarGallery indexerBar;
	private TabItem tab;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main_shop);
		findViewById(R.id.title_back).setVisibility(View.GONE);
		findViewById(R.id.search_input).setOnClickListener(this);
		((TextView) findViewById(R.id.activity_title))
				.setText(R.string.shop_text);

		initGallery(WiseSiteApplication.getContext().getModelResult());
		headlineShop = new InfoListAdatper(ITEM_TYPE.SHOP_TYPE, this);
		mListView = (ListView) findViewById(R.id.list);
		headlineShop.setListView(mListView);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int index,
					long id) {
				ShopItem item = (ShopItem) v.getTag();
				if (item != null) {
					Intent intent = new Intent(getApplicationContext(),
							ShopsDetailsActivity.class);
					intent.putExtra(Constanct.INFO_ENTRY, item);
					startActivity(intent);
				}
			}
		});

		indexerBar.setSelection(Integer.MAX_VALUE / 2
				- ((Integer.MAX_VALUE / 2) % tab.indexBarList.size()));

		int typeId = WiseSiteApplication.getContext().getModelResult()
				.getTabItem(3).indexBarList.get(0).id;
		int i = 0;
		for (IndexerBarEntry e : WiseSiteApplication.getContext()
				.getModelResult().getTabItem(3).indexBarList) {
			if (e.type == 2) {
				typeId = e.id;
				break;
			}
			i++;
		}
		indexerBar.toIndex(i);

		headlineShop.setType(typeId);
		headlineShop.moveToFristPage();
		headlineShop.loadData();

	}

	private void initGallery(ModelResult result) {
		tab = result.getTabItem(3);
		if (tab.indexBarList == null || tab.indexBarList.size() == 0) {
			return;
		}

		indexerBar = (IndexerBarGallery) findViewById(R.id.index_gallery);
		indexerBar.setTabId(tab.id);
		mGalleryAdapter = new IndexerBarAdapter(this);
		mGalleryAdapter.setList(tab.indexBarList);
		indexerBar.setAdapter(mGalleryAdapter);
		indexerBar.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View v, int arg2,
					long arg3) {
				headlineShop.removeAll();
				indexerBar.setSelector(v);

				IndexerBarEntry entry = (IndexerBarEntry) v.getTag();
				headlineShop.setType(entry.id);
				headlineShop.moveToFristPage();
				headlineShop.loadData();
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.search_input: {
			Intent intent = new Intent(getApplicationContext(),
					SearchActivity.class);
			intent.putExtra(Constanct.INTENT_SEARCH_TYPE, 1);
			startActivity(intent);
			break;
		}
		default:
			break;
		}
	}
}
